package {
	import states.GamePlayState;
	import org.flixel.FlxObject;
	/**
	 * @author NilsIH
	 */
	public class NiloTile extends FlxObject{
		
		private static var _tilesheet:Array;
		private static var _tilesWidth:uint = 16;
		private static var _tilesHeight:uint = 11;
		private static var _hospitalTiles:Array;
		public static function reset():void {
			_tilesheet = null;
			_hospitalTiles = null;
		}
		public static function get totalTiles():uint {
			return _tilesWidth * _tilesHeight;
		}
		public static function get hospitalTiles():Array {
			return _hospitalTiles;
		}
		public static function get tilesheet():Array {
			if (_tilesheet == null){
				_tilesheet = new Array();
				var i:uint = 0;
				for (i = 0; i < _tilesWidth * _tilesHeight; i++){
					var tile:NiloTile = new NiloTile();
					tile.sheetIndex = i;
					_tilesheet.push(tile);
				}
				
				getTileAtTileCoordinates(7, 3).tileType = "hospital";
				getTileAtTileCoordinates(7, 3).layeredAbove = true;
				getTileAtTileCoordinates(8, 3).tileType = "hospital";
				getTileAtTileCoordinates(8, 3).layeredAbove = true;
				getTileAtTileCoordinates(5, 4).tileType = "hospital";
				getTileAtTileCoordinates(5, 4).layeredAbove = true;
				getTileAtTileCoordinates(6, 4).tileType = "hospital";
				getTileAtTileCoordinates(6, 4).layeredAbove = true;
				getTileAtTileCoordinates(7, 4).tileType = "hospital";
				getTileAtTileCoordinates(8, 4).tileType = "hospital";
				getTileAtTileCoordinates(9, 4).tileType = "hospital";
				getTileAtTileCoordinates(9, 4).layeredAbove = true;
				getTileAtTileCoordinates(10, 4).tileType = "hospital";
				getTileAtTileCoordinates(10, 4).layeredAbove = true;
				getTileAtTileCoordinates(5, 5).tileType = "hospital";
				getTileAtTileCoordinates(6, 5).tileType = "hospital";
				getTileAtTileCoordinates(7, 5).tileType = "hospital";
				getTileAtTileCoordinates(8, 5).tileType = "hospital";
				getTileAtTileCoordinates(9, 5).tileType = "hospital";
				getTileAtTileCoordinates(10, 5).tileType = "hospital";
				getTileAtTileCoordinates(5, 6).tileType = "hospital";
				getTileAtTileCoordinates(6, 6).tileType = "hospital";
				getTileAtTileCoordinates(7, 6).tileType = "hospital";
				getTileAtTileCoordinates(8, 6).tileType = "hospital";
				getTileAtTileCoordinates(9, 6).tileType = "hospital";
				getTileAtTileCoordinates(10, 6).tileType = "hospital";
				getTileAtTileCoordinates(7, 7).tileType = "hospital";
				getTileAtTileCoordinates(8, 7).tileType = "hospital";
				_hospitalTiles = [];
				for (i = 0; i < _tilesheet.length; i++){
					if ((_tilesheet[i] as NiloTile).tileType == "hospital"){
						_hospitalTiles.push(_tilesheet[i]);
					}
				}
			}
			
			return NiloTile._tilesheet;
		}
		
		private static function checkIndex(index:int):Boolean{
			if (index < 0) return false;
			if (index >= _tilesWidth * _tilesHeight) return false;
			return true;
		}
		
		public static function getTileAt(x:Number, y:Number):NiloTile {
			var x1:int = Math.floor(x / 50);
			var y1:int = Math.floor(y / 50);
			return _tilesheet[(_tilesWidth * y1) + x1];
		}
		
		public static function getTileAtTileCoordinates(x:Number, y:Number):NiloTile {
			return _tilesheet[(_tilesWidth * y) + x];
		}
		public var tileType:String = "default";
		public var layeredAbove:Boolean = false;
		
		public function set sheetIndex(i:uint):void {
			_sheetIndex = i;
			this.y = Math.floor(i / _tilesWidth) * 50;
			this.x = (i % _tilesWidth) * 50;
			
			_adjacentIndices = new Array();
			var index:int = 0;
			//	Top
			index = _sheetIndex - _tilesWidth;
			if (checkIndex(index)){
				_adjacentIndices.push(index);
			}
			
			//	Left
			index = (_sheetIndex % _tilesWidth) - 1;
			if (index >= 0){
				_adjacentIndices.push(_sheetIndex - 1);
			}
			
			//	Bottom
			index = _sheetIndex + _tilesWidth;
			if (checkIndex(index)){
				_adjacentIndices.push(index);
			}
			
			//	Right
			index = (_sheetIndex % _tilesWidth) + 1;
			if (index < _tilesWidth){
				_adjacentIndices.push(_sheetIndex + 1);
			}
		}
		
		public function infuseTileWith(action:String):void {
			
			if (tileType == "hospital"){
				var result:Boolean = GamePlayState.hospital.doAction(action,(this.x / 50) - 5, (this.y / 50) - 3);
				if (action == "explode" && !result){
					action = "passiveexplode";
				}
			}
			
			if (tileEffect == null){
				tileEffect = new TileEffect().initialize(this, action);
				
			}
			else {
				tileEffect.doAction(action);
			}
			if (action == "passiveexplode"){
				tileEffect = null;
			}
			else {
				//tileEffect.determineGFX();
				//determineAdjacent();
			}
		}
		
		public function determineAdjacent():void {
			for (var i:uint = 0; i < adjacentTiles.length; i++){
				var niloTile:NiloTile = adjacentTiles[i];
				if (niloTile.tileEffect != null) niloTile.tileEffect.determineGFX();
			}
		}
		
		public function tileRecovered():void{
			if (tileType == "hospital"){
				GamePlayState.hospital.horribleDeedDone(tileEffect.type, (this.x / 50) - 5, (this.y / 50) - 3);
			}
		}
		
		
		public function get adjacentTiles():Array {
			if (_adjacentTiles == null){
				_adjacentTiles = new Array();
				for (var i:uint = 0; i < _adjacentIndices.length; i++){
					_adjacentTiles.push(_tilesheet[_adjacentIndices[i]]);	
				}
			}
			return _adjacentTiles;
		}
		
		public function get topTile():NiloTile{
			//	Top
			var index:int = _sheetIndex - _tilesWidth;
			if (checkIndex(index)){
				return _tilesheet[index];
			}
			return null;
		}
		
		public function get leftTile():NiloTile{
			//	Left
			var index:int = (_sheetIndex % _tilesWidth) - 1;
			if (index >= 0){
				return _tilesheet[_sheetIndex - 1];
			}
			return null;
		}
		
		public function get bottomTile():NiloTile{
			//	Bottom
			var index:int = _sheetIndex + _tilesWidth;
			if (checkIndex(index)){
				return _tilesheet[index];
			}
			return null;
		}
		
		public function get rightTile():NiloTile{
			//	Right
			var index:int = (_sheetIndex % _tilesWidth) + 1;
			if (index < _tilesWidth){
				return _tilesheet[_sheetIndex + 1];
			}
			return null;
		}
		private var _sheetIndex:uint;
		private var _adjacentIndices:Array;
		private var _adjacentTiles:Array;
		public var tileEffect:TileEffect;
		
	}
}
